$(document).ready(function(){
	
	$.validator.addMethod("minFloor",function(value,element,params){
		if(value=="") return true;
		if(parseInt(value)<parseInt(params.val())){
			return false;
		}
		return true;
	},"不得低于楼层数");
	
	$.validator.addMethod("infoId" , function(value ,element , params){
		if(!(/^[0-9a-zA-Z]*$/.test(value))){
			return false;
		}
		return true;
	},"请输入数字或字母");

	if($("#publishform")){
		$("#publishform").validate({
			rules: {
				/*title: {
					required: true,
					maxlength:100
				},*/
				infoid:{
					infoId:true
				},
				plaza: {
					required: true,
					maxlength:50
				},
				square: {
					required: true,
					number:true
				},
				style1: {
					required: true,
					digits:true
				},
				style2: {
					required: true,
					digits:true
				},
				style3: {
					required: true,
					digits:true
				},
				floor1: {
					required: true,
					digits:true
				},
				floor2: {
					minFloor : $("#floor1"),
					digits:true
				},
				age: {
					digits:true
				},
				price: {
					required: true,
					number:true
				},
				detail: {
					maxlength:200
				}
			},
			messages: {
				/*title: {
					required: "请输入标题",
					maxlength:"标题不要超过100个字"
				},*/
				infoid:{
					infoId:"请输入数字或字母"
				},
				plaza: {
					required: "请输入小区名",
					maxlength:"小区名不要超过50个字"
				},
				square: {
					required: "请输入面积",
					number:"请填入阿拉伯数字"
				},
				style1: {
					required: "请输入几室",
					digits:"请填入阿拉伯数字"
				},
				style2: {
					required: "请输入几厅",
					digits:"请填入阿拉伯数字"
				},
				style3: {
					required: "请输入几卫",
					digits:"请填入阿拉伯数字"
				},
				floor1: {
					required: "请输入楼层",
					digits:"请填入阿拉伯数字"
				},
				floor2: {
					minFloor : "不得低于楼层数",
					digits:"请填入阿拉伯数字"
				},
				age: {
					digits:"请填入阿拉伯数字"
				},
				price: {
					required: "请输入售价",
					digits:"请填入阿拉伯数字"
				},
				detail: {
					maxlength:"描述不要超过200个字"
				}
			},
			submitHandler: function(form) {  
				$("#buttondiv").css("display" , "none");
				$("#fbmsg").css("display" , "");
				form.submit();  
			}

		});

		$("#submitbtn2").bind("click" , function(){
			$("#ispublic").val(1);
			$("#publishform").trigger("submit");
		});
	}
	
	if($("#infoid")){
		initTips($("#infoid"));
	}
	
	if($("#maintitle")){
		$("#maintitle").bind("click" , function(e){
			var el = e.target;
			var tag = el.tagName;
			if(tag=="INPUT"){
				$("#ispublic").val($(el).val());
				submitForm();
			}
		});
	}
	
	if($("#seachidbtn")){
		$("#seachidbtn").bind("click" , function(){
			submitForm();
		});
	}
	
	if($("#ordersel")){
		$("#ordersel").bind("change" , function(){
			$("#order").val($(this).val());
			submitForm();
		});
		
		$("#ordersel").val($("#order").val());
	}
	
	if($("#nextpage")){
		$("#nextpage").bind("click" , function(){
			var np = $min(parseInt($("#r_page").val())+1 , parseInt($("#totalpage").val()));
			$("#r_page").val(np);
			submitForm();
		});
		if($("#r_page").val()==$("#totalpage").val()){
			$("#nextpage").removeAttr("href");
		}else{
			$("#nextpage").attr("href" , "###");
		}
	}
	
	if($("#prepage")){
		$("#prepage").bind("click" , function(){
			var pp = $max(parseInt($("#r_page").val())-1 , 1);
			$("#r_page").val(pp);
			submitForm();
		});
		if($("#r_page").val()=="1"){
			$("#prepage").removeAttr("href");
		}else{
			$("#prepage").attr("href" , "###");
		}
	}
	
	if($("#checkAll1")){
		$("#checkAll1").bind("click" , selectAll);
		$("#checkAll2").bind("click" , selectAll);
	}
	
	if($("#upload1")){
		$("#upload1").bind("click" , uploadAll);
		$("#upload2").bind("click" , uploadAll);
	}
	
	if($("#status")&&$("#status").val()=="edit"){
		$("#lx").val($("#h_lx").val());
		$("#area").val($("#h_area").val());
		$("#style1").val($("#h_style1").val());
		$("#style2").val($("#h_style2").val());
		$("#style3").val($("#h_style3").val());
		$("#decoration").val($("#h_decoration").val());
		var str = $("#h_mstyle").val().split("|");
		if(str[0]=="ck"){$("#mstyle1").attr("checked" , true);}
		if(str[1]=="gl"){$("#mstyle2").attr("checked" , true);}
		var ips = $('input[name=decoration]:radio');
		for(var i=0 , l = ips.length ; i<l ;i++){
			if(ips[i].value == $("#h_decoration").val()){
				ips[i].checked = true;
			}
		}
	}

	$("#showcompare").bind("click" , function(){
		var plaza = $("#plaza").val();
		if(plaza==""){alert("请输入小区名称");}
		else{
			window.open("compare_shadd.php?plaza="+plaza);
		}
	});

});

function submitForm(){
	if($("#listform")){
		if($("#infoid")&&$("#infoid").val()==$("#infoid").attr("tips")){$("#infoid").val("");}
		$("#listform").trigger("submit");
	}
}

function selectAll(){
	var status = this.checked;
	$("#uploadlist input[type='checkbox']").attr("checked", status); 
}

function uploadAll(){
	var ips = $("input[name='pchecker']");
	var arr = [];
	var upordown = $("#ispublic").val()=="0"?1:0;
	for(var i=0 , l = ips.length ; i<l ;i++){
		if(ips[i].checked==true){
			arr.push(ips[i].value);
		}
	}
	var ids = arr.join(",");
	$.get($("#sorr").val()+"house_post.php", { action: "update", ids: ids , upordown : upordown },
	  function(data){
		if(data=="success"){
			location.href = $("#hidelink").val();
		}
	  }); 
}